﻿using MediatR;
using Traceability.APP.Application.Command.Shop;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrerture.Interfaces;

namespace Traceability.APP.Application.CommandHandle.Shop
{
    /// <summary>
    /// 商品信息的新增
    /// </summary>
    public class GetAddShopCommandHandle:IRequestHandler<GetAddShopCommand,APIResult<Shop_Model>>
    {
        private readonly IBaseRepository<Shop_Model> ShopDal;

        public GetAddShopCommandHandle(IBaseRepository<Shop_Model> shopDal)
        {
            ShopDal = shopDal;
        }

        public async Task<APIResult<Shop_Model>> Handle(GetAddShopCommand request, CancellationToken cancellationToken)
        {
            APIResult<Shop_Model> result = new APIResult<Shop_Model>();
            var rolename = ShopDal.GetAll().Where(x => x.Shop_Name == request.Shop_Name).FirstOrDefault();
            if (rolename != null)
            {
                result.Code = StateCode.Faile;
                result.Message = "角色名称已存在";
                return result;
            }

            Shop_Model shop = new Shop_Model();
            shop.SID = request.SID;
            shop.Shop_Name = request.Shop_Name;
            
            await ShopDal.Add(shop);

            result.Code = StateCode.Ok;
            result.Message = StateCode.Ok.ToString();
            result.Data = shop;
            return result;


        }


    }
}
